import java.util.Arrays;

public class ArrayOrder {

    public static void main(String[] args) {

        // 创建一个数组
        int[] array = {8, 2, 3, 9, 5, 4, 10, 7, 30};

        // 从小到大排序
        System.out.println("没有排序前的顺序：" + Arrays.toString(array));
        order1(array);
        System.out.println("按从大到小排序:" + Arrays.toString(array));
        order2(array);
        System.out.println("按从小到大排序:" + Arrays.toString(array));

//        int a = 10;
//        int b = 20;
//        System.out.println("a = " + a + ",b = " + b);
//
//
//        int temp = 0;
//        temp = a;
//        a = b;
//        b = temp;
//        System.out.println("a = " + a + ",b = " + b);

    }

    // 冒泡排序的核心在和相邻两个元素做比对
    // 从小到大排序
    private static void order1(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] < array[j + 1]) {
                    int temp = 0;
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    private static void order2(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = 0;
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }
}
